package com.hornsun.repository;

import com.google.common.base.Preconditions;
import com.hornsun.util.StrUtil;
import com.hornsun.domain.dbo.Thumbnail;
import com.hornsun.domain.dbo.ThumbnailExample;
import com.hornsun.repository.mapper.ThumbnailMapper;
import com.hornsun.util.LittleUtil;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by 编译中... on 2018/5/16.
 */
@Repository
public class ThumbnailRepository {
    @Resource
    private ThumbnailMapper thumbnailMapper;

    @Resource
    private LittleUtil littleUtil;

    /**
     * 插入缩略图
     * @param thumbnail
     */
    public void insert(Thumbnail thumbnail){
        int count = thumbnailMapper.insertSelective(thumbnail);
        if (count!=1){
            throw new IllegalArgumentException("insert thumbnail failed  by thumbnail"+thumbnail);
        }
    }


    /**
     * 分页获取缩略图
     * @param page
     * @return
     */
    public List<Thumbnail> getThumbnails(int page){
        ThumbnailExample thumbnailExample = new ThumbnailExample();
        thumbnailExample.setOrderByClause(StrUtil.toDesc("upload_time"));
        return thumbnailMapper.selectByExampleWithRowbounds(new ThumbnailExample(),littleUtil.bigRowBounds(page));
    }

    /**
     * 获取缩略图总数
     * @return
     */
    public long getTotalCount(){
        return thumbnailMapper.countByExample(new ThumbnailExample());
    }

    /**
     * 删除缩略图
     * @param thumbnailGuid
     */
    public void deleteByGuid(String thumbnailGuid){
        Preconditions.checkNotNull(thumbnailGuid);
        int count = thumbnailMapper.deleteByPrimaryKey(thumbnailGuid);
        if (count!=1){
            throw new IllegalArgumentException("delete thumbnail failed by thumbnailGuid:"+thumbnailGuid);
        }
    }

}
